import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/application/Inventory_system/get_feign_material_points_list_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/application/Inventory_system/get_feign_page_detail_entity.dart';

///应急物资清单
class EmergencyMaterialListController extends GetxController {
  /// 筛选右侧栏控制Key
  final riskRightScaffoldKey = GlobalKey<ScaffoldState>();

  /// 列表刷新key
  final GlobalKey<CustomEasyRefreshListState> globalKey = GlobalKey();

  String responsiblePerson = '';
  String searchResponsiblePerson = '';
  String contactNumber = '';
  String searchContactNumber = '';

  String searchSelect = '';
  RxString searchX = ''.obs;

  /// 获取数据
  Future<List<GetFeignPageDetailEntity>> feignPageDetail(int index, int size, {String? search}) async {
    final res = await HttpUtil().generateGetRequest<List<GetFeignPageDetailEntity>>(
      Interface.getFeignPageDetail,
      queryParameters: {
        'pageNum': index,
        'pageSize': size,
        'materialName': searchX.value, //	物资名称
        'responsiblePerson': responsiblePerson, //	负责人
        'contactNumber': contactNumber, //联系方式
        'materialPointIds': materialPointIds, //物资点ID集合
      },
    ).execute();
    List<GetFeignPageDetailEntity>? list = [];
    res.onSuccess((result) {
      if (result.data != null) {
        list = result.data;
      }
    });
    return list!;
  }

  /// 筛选按钮
  void onFilterButtonPressed() {
    searchSelect = searchX.value; //向另一个地方插入值 那个地方也和这一样
    riskRightScaffoldKey.currentState?.openEndDrawer();
  }

  /// 重置
  void reset() {
    materialPointIds.clear();
    //获取物资点 清空
    for (var value in getFeignMaterialPointsListEntityList) {
      if (value.checkboxSelected) {
        value.checkboxSelected = false;
      }
    }
    searchX.value = '';
    searchSelect = '';
    responsiblePerson = '';
    searchResponsiblePerson = '';
    contactNumber = '';
    searchContactNumber = '';
    riskRightScaffoldKey.currentState?.closeEndDrawer();
    globalKey.currentState!.onRefresh();
    update();
  }

  /// 'materialPointIds': '', //物资点ID集合
  List<String> materialPointIds = [];

  /// 确定
  void confirm() {
    materialPointIds.clear();
    //获取物资点
    for (var value in getFeignMaterialPointsListEntityList) {
      if (value.checkboxSelected) {
        //加入
        // materialPointIds += '${value.id};';
        materialPointIds.add(value.id ?? '');
      }
    }
    Get.back();
    searchX.value = searchSelect;
    responsiblePerson = searchResponsiblePerson;
    contactNumber = searchContactNumber;
    globalKey.currentState!.onRefresh();
    update();
  }

  @override
  void onInit() {
    super.onInit();
    getFeignMaterialPointsList();
  }

  List<GetFeignMaterialPointsListEntity> getFeignMaterialPointsListEntityList = [];

  ///应急物资点列表
  getFeignMaterialPointsList() async {
    final res = await HttpUtil()
        .generateGetRequest<List<GetFeignMaterialPointsListEntity>>(
          Interface.getFeignMaterialPointsList,
        )
        .execute();
    res.onSuccess((result) {
      if (result.data != null) {
        getFeignMaterialPointsListEntityList = result.data ?? [];
      }
    });
    update();
  }

  void refreshPage() {
    globalKey.currentState?.onRefresh();
  }
}
